import sys
import os
import tkinter as tk
from tkinter import StringVar
import random
from PIL import Image, ImageTk

# 初始化Tkinter窗口
root = tk.Tk()
root.title("全屏滚动抽奖程序")
root.attributes("-fullscreen", True)  # 设置全屏

def resource_path(relative_path):
    """ Function for including images and excel files in the executable file """
    if hasattr(sys, '_MEIPASS'):
        return os.path.join(sys._MEIPASS, relative_path)
    return os.path.join(os.path.abspath("."), relative_path)


# 设置背景图片
background_image_path = resource_path("bg.png")  # 请确保图片路径正确
background_image = Image.open(background_image_path)
background_photo = ImageTk.PhotoImage(background_image)
background_label = tk.Label(root, image=background_photo)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

# 滚动文字显示
scroll_text_var = StringVar()
scroll_text_var.set("抽奖即将开始！")
# scroll_label = tk.Label(root, textvariable=scroll_text_var, justify='center', bg="#134388", fg='white', wraplength=800, font=('Helvetica', 100, 'bold'))
scroll_label = tk.Label(root, textvariable=scroll_text_var, justify='center', bg="red",  fg='yellow', wraplength=1000, font=('Helvetica', 100, 'bold'))
scroll_label.pack(pady=350)

# 抽奖名单写在这里
participants = ["刘玄德", "曹孟德", "孙策", "孙权", "诸葛孔明", "周公瑾", "关云长", "张翼德", "赵子龙"]

winners = []
going = False
is_run = False




def draw_lottery():
    global going, is_run
    if not going:
        going = True
        is_run = True
        scroll_text_var.set("正在抽奖...")
        root.after(100, scroll_text)  # 开始滚动文字

def scroll_text():
    global going, is_run, winner
    if going:
        if len(winners) == len(participants):
            scroll_text_var.set("全抽到了！")
            is_run = False
        else:
            while True:
                winner = random.choice(participants)
                if winner not in winners:
                    break
            scroll_text_var.set(winner)
            root.after(100, scroll_text)  # 继续滚动文字
    else:
        is_run = False

def stop_lottery():
    global going, winner
    going = False
    # print("winners:", len(winners), winners)
    if winner not in winners:
        winners.append(winner)
    # print("current winner:", winner)


def exit():
    sys.exit(0)

# 开始和结束按钮
start_button = tk.Button(root, text="开始", command=draw_lottery, font=('Helvetica', 30), bg='red')
start_button.pack(side=tk.LEFT, expand=True, fill=tk.X, padx=200)

stop_button = tk.Button(root, text="抽奖", command=stop_lottery, font=('Helvetica', 30), bg='red')
stop_button.pack(side=tk.LEFT, expand=True, fill=tk.X, padx=200)

exit_button = tk.Button(root, text="退出", command=exit, font=('Helvetica', 30), bg='red')
exit_button.pack(side=tk.LEFT, expand=True, fill=tk.X, padx=200)

# 运行主循环
root.mainloop()